#include <stdint-gcc.h>
#include <stddef.h>
#include "packet.h"
#include "ethmac.h"
#include "uart.h"
#include "timer.h"

/* MUST NOT RETURN */
void main(void)
{
	/* Enable the serial debug port */
	uart_init();
	//puts("myarm debug port\r");

	/* initialize the memory allocation system */
	//init_malloc();

	/* Enable the hardware timer to generate interrrupts 100 times per second,
	   current time will start incrementing from this point onwards */
	//init_timer();

	/* Create a timer to flash a led periodically */
	//init_led();

	/* initialize the PHY and MAC and listen for connections
	   This is the last init because packets will be received from this point
	   onwards. */
#if 1
	init_ethmac();

	puts("Ethernet MAC initialized\r");
#else
	uint32_t moder = ethmac_moder;
	moder |= 2; // ETHMAC_MODER_TXEN
	ethmac_moder = moder;
	ethmac_bufdesc[1] = (uint32_t)eth_tx_buffer;
	ethmac_tx_packet(0, 0x100);
#endif

	/* create a tcp socket for listening on port 23 */
	//listen_telnet();

	/* initialize the tftp stuff */
	//init_tftp();

	timer_reload = 99999999; // 每秒1次 @ 100MHz
	timer_en = 1;

	/* Process loop. Everything is timer, interrupt and queue driven from here on down */
	//puts("Entering event loop");
	//putchar('\r');

	asm("cpsie i");
	while (1) {

		/* flash an led */
		//process_led();

		/* Check for received tftp files and reboot */
		//process_tftp();

		/* Process all socket traffic */
		//process_sockets();
	}
}
